class GraspService {

  boolean transactional = true

  def serviceMethod() {

  }

  def grasp(String url, Entry entryInstance) {
    if (url.indexOf('babycenter.com') > 0) {
      entryInstance.source = 'babycenter'
      if(url.indexOf('tools/') > 0){
        return
      }
      return graspBabyCenter(url, entryInstance)
    } else if (url.indexOf('ci123.com') > 0){
      entryInstance.source = 'ci123'
      return graspCi123(url, entryInstance)
    } else {
      return
    }
  }

  def graspBabyCenter(String url, Entry entryInstance) {
    def parser = new org.cyberneko.html.parsers.SAXParser()
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    def buffer = new StringBuffer()
    def chapter = new XmlParser(parser).parse(url)
    def chapterDate = chapter.depthFirst().DIV.grep {it.'@class'?.startsWith('mainContentPad')}
    def breakhere = false
    chapterDate[0].children().each {
      if (breakhere) {
      } else if (it instanceof String) {
        buffer << it
      } else if (it.name() == "BR") {
        buffer << "<br/>"
      } else if (it.name() == "DIV") {

      } else if (it.name() == "SCRIPT") {

      } else if (it.name() == "H1") {
        entryInstance.title = it.text()
      } else if (it.name() == "H2") {
        buffer << "<h2>" << it.text() << "</h2>"
      } else if (it.name() == "P") {
        if (!it.'@id') {
          it.children().each {child ->
            if (child instanceof String) {
              buffer << child
            } else if (child.name() == "BR") {
              buffer << "<br/>"
            } else if (child.name() == "A") {
              if (child.'@href'.startsWith('/about/')) {
              } else {
                println  child.'@href'
                buffer << child.text()
              }
            }
          }
        }
      } else if (it.name() == "A") {
        //if (it.'@name' == '12') breakhere = true
      } else {
        buffer << it.text()
      }
    }
    entryInstance.content = buffer.toString()
  }
  def graspCi123(String url, Entry entryInstance) {
    def parser = new org.cyberneko.html.parsers.SAXParser()
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    def buffer = new StringBuffer()
    def chapter = new XmlParser(parser).parse(url)
    def titleData = chapter.depthFirst().H1
    if(titleData)
      entryInstance.title = titleData[0].text()
    def chapterDate = chapter.depthFirst().BLOCKQUOTE
    if(!chapterDate)
      chapterDate = chapter.depthFirst().DIV.grep {it.'@id'?.startsWith('articletext')}
    def breakhere = false
    chapterDate[0].children().each {
      if (breakhere) {
      }  else if (it instanceof String) {
        buffer << it
      } else if (it.name() == "P"){
        it.children().each {child ->
          if(child instanceof String) {
            buffer << child
          } else if (child.name() == "BR"){
            buffer << '<br/>'
          }
        }
        buffer << '<br/>'
      } else {
        buffer << it.text()
      }
    }
    entryInstance.content = buffer.toString()
  }

}
